package async;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author fei.fang
 * @date 2021/8/27
 **/
public class CountDownLatchTest implements Runnable{

    private static final CountDownLatch down = new CountDownLatch(10);

    @Override
    public void run() {
        try {
            Thread.sleep(new Random().nextInt(10) * 1000);
            System.out.println("check complete");
            down.countDown();
            System.out.println(down.getCount());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService exec = Executors.newFixedThreadPool(10);
        CountDownLatchTest test = new CountDownLatchTest();
        for (int i = 0; i < 20; i++) {
            exec.execute(test);
        }
        down.await();
        System.out.println("fire");
        exec.shutdown();
    }
}
